package com.itsv.news.adapter;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.OnGestureListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itsv.news.R;
import com.itsv.news.bean.NewsBean;
import com.itsv.news.bean.NewsBean;
import com.itsv.news.utils.AsyncImageLoader;

public class ListmsgAdapter extends BaseAdapter implements OnGestureListener{
	public Context con = null;
	private List<NewsBean> dl;
	private AsyncImageLoader ail=AsyncImageLoader.getInstance();
	private LayoutInflater inflater = null;
	private final String TAG="ListAdapter";
	public ListmsgAdapter(Context con,List<NewsBean> datalist) {
		this.con = con;
		inflater = LayoutInflater.from(con);
		this.dl=datalist;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return dl.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if(convertView!=null){
			viewHolder = (ViewHolder)convertView.getTag();
		} else {
			viewHolder =new ViewHolder();
			convertView = inflater.inflate(R.layout.list_item, null);
			viewHolder.tv_title=(TextView) convertView.findViewById(R.id.tv_title);
			viewHolder.tv_date=(TextView) convertView.findViewById(R.id.tv_date);
			viewHolder.tv_desc=(TextView) convertView.findViewById(R.id.tv_desc);
			viewHolder.iv_pic=(ImageView)convertView.findViewById(R.id.tv_pic);
			viewHolder.iv_pic.setPadding(10, 10,10,20);
			convertView.setTag(viewHolder);
		}
	
		viewHolder.tv_title.setText(dl.get(position).getTitles());
		viewHolder.tv_date.setText(dl.get(position).getPublicTime().split(" ")[0]);
//		Log.i(TAG,"length==>"+dl.get(position).getIntro().length() );
//		Log.i(TAG,dl.get(position).getIntro() );
		if(dl.get(position).getIntro().length()<17)
			viewHolder.tv_desc.setText(dl.get(position).getIntro()+"\r\n");
		else
			viewHolder.tv_desc.setText(dl.get(position).getIntro());
		Log.i(TAG, "---"+dl.get(position).getTitlePic());
		ail.displayImage(dl.get(position).getTitlePic(), viewHolder.iv_pic,true);
		return convertView;
	}


	private static class ViewHolder {
		private TextView tv_desc;
		private TextView tv_date;
		private TextView tv_title ;
		private ImageView iv_pic;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		//		if(velocityY!=0){
		//			return true;
		//		}
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		Log.v(TAG, "distanceY:"+distanceY);
		if(distanceY!=0){
			return true;
		}
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return true;
	}
	
}
